package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.mapper.MemberCardMapper;
import com.woniu.pojo.Member;
import com.woniu.mapper.MemberMapper;
import com.woniu.service.IMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.vo.MemberQueryVo;
import com.woniu.vo.QueryCardVo;
import com.woniu.vo.QueryMemberLevelCoachVo;
import com.woniu.vo.QueryMemberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xie
 * @since 2022-10-12
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    @Autowired
    private MemberMapper mapper;

    @Autowired
    private MemberCardMapper memberCardMapper;


    @Override
    public PageResult queryByItemInPage(int currentPage, int pageSize, MemberQueryVo vo) {
        IPage ipage=new Page(currentPage,pageSize);
        Page<QueryMemberVo> result=mapper.selectByItemInPage(ipage,vo);
        List<QueryMemberVo> records = result.getRecords();
        for (int i = 0; i < records.size(); i++) {
            List<QueryCardVo> cardList = mapper.selectCardById(records.get(i).getId());
            records.get(i).setCard(cardList);
        }
        return new PageResult(result.getTotal(),records);
    }

    @Override
    public List<QueryCardVo> queryCardById(String id) {
        return mapper.selectCardById(id);
    }

    @Override
    public QueryMemberLevelCoachVo queryMemberLevelCoachById(String id) {
        return mapper.selectMemberLevelCoachById(id);
    }

    @Override
    public int delete(String id) {
        int i = mapper.deleteById(id);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("member_id",id);
        int delete = memberCardMapper.delete(wrapper);
        return delete+i;
    }

    @Override
    public String queryMemberId(String tel) {
        return mapper.selectMemberId(tel);
    }

}
